﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZilLion.Service.Crm.CommonRepository;
using ZilLion.Service.Crm.ModelBase.EntityBase.Config;
using ZilLion.Service.Crm.ModelBase.EntityBase.Error;
using ZilLion.Service.Crm.ModelBase.EntityBase.Identity;
using ZilLion.Service.Crm.Unities.Cache;
using ZilLion.Service.Crm.Unities.Config;
using ZilLion.Service.Crm.Unities.IOC;
using ZilLion.Service.Crm.Unities.Other;
using ZilLion.Service.WebInfrastructure.Base;
using ZilLion.Service.WebInfrastructure.Filter;
using ZilLion.Service.WechatService.Domain;
using ZilLion.Service.WechatService.Domain.WebApiEntity.CRM;
using ZilLion.Service.WechatService.Domain.WebApiRepository.InterFace;

namespace ZilLion.Service.WechatService.Controllers
{
    [Route("Api/Config")]
    [AllowAnonymous]
    //[AuthorizationConfigValidateAtrribute]
    public class ConfigController : WebApiBase
    {
        [Route("GetConfigItem")]
        [HttpGet]
        public ConfigGroup GetConfigItem(string jappid)
        {
            IGloabConfigHandler ConfigHandler = new GloabConfigHandler(new WechatServiceContext());
            ConfigGroup cg = ConfigHandler.GetConfigItem(jappid);
            return cg;
        }

        [Route("CheckConfigItems")]
        [HttpGet]
        public ResponseResultInfo CheckConfigItems()
        {
            IGloabConfigHandler ConfigHandler = new GloabConfigHandler(new WechatServiceContext());
            int groupVersion2 = ConfigHandler.CheckConfigItems();
            return new ConfigGroup() {logsn = groupVersion2 };
        }

        [Route("GetAppidjoininfos")]
        [HttpGet]
        public AconfigGroup GetAppidjoininfos(string jappid,int type)
        {
            AconfigGroup Group =new AconfigGroup();
            var AppidjoininfoRepository = ClassFactory.GetInstance<IAppidjoininfoRepository>();
            if (type == 0)
            {
                Group = AppidjoininfoRepository.GetAppidjoininfos(jappid);
            }
            else
            {
                Group = AppidjoininfoRepository.GetAppidjoininfosForAdmin(jappid);
            }
            return Group;
        }

        [Route("InsertAppidjoininfo")]
        [HttpPost]
        public ResponseResultInfo InsertAppidjoininfo(AconfigItem nitem)
        {
            var AppidjoininfoRepository = ClassFactory.GetInstance<IAppidjoininfoRepository>();
            if(AppidjoininfoRepository.Checkappidkey(nitem)) return new ResponseResultInfo() {Errormodel = new ErrorModel() {desc = "配置键已经存在！"} };
            AppidjoininfoRepository.InsertAppidjoininfo(nitem);
            return new ResponseResultInfo() ;
        }

        [Route("UpdateAppidjoininfo")]
        [HttpPost]
        public ResponseResultInfo UpdateAppidjoininfo(AconfigItem nitem)
        {
            var AppidjoininfoRepository = ClassFactory.GetInstance<IAppidjoininfoRepository>();
            //if (AppidjoininfoRepository.Checkappidkey(nitem)) return new ResponseResultInfo() { Errormodel = new ErrorModel() { desc = "配置键已经存在！" } };
            AppidjoininfoRepository.UpdateAppidjoininfo(nitem);
            return new ResponseResultInfo();
        }

        [Route("DeleteAppidjoininfo")]
        [HttpPost]
        public ResponseResultInfo DeleteAppidjoininfo(AconfigItem nitem)
        {
            var AppidjoininfoRepository = ClassFactory.GetInstance<IAppidjoininfoRepository>();
            AppidjoininfoRepository.DeleteAppidjoininfo(nitem);
            return new ResponseResultInfo();
        }

        [Route("GetAppids")]
        [HttpGet]
        public AconfigGroup GetAppids()
        {
            var AppidjoininfoRepository = ClassFactory.GetInstance<IAppidjoininfoRepository>();
            AconfigGroup item = AppidjoininfoRepository.GetAppids();
            return item;
        }

        [Route("GetAppidjoininfoKey")]
        [HttpGet]
        public AconfigGroup GetAppidjoininfoKey(string jappid, string key)
        {
            var AppidjoininfoRepository = ClassFactory.GetInstance<IAppidjoininfoRepository>();
            AconfigGroup item = AppidjoininfoRepository.GetAppidjoininfoKey(jappid, key);
            return item;
        }

        [Route("GetTicket")]
        [HttpGet]
        public string GetTicket(string name, string password)
        {
            string ticket = DigestHelper.SHA1_Encrypt(name + password + DateTime.Now.ToString("yyyy-MM-dd HH"));
            return ticket;
        }

        [Route("AddModelToCache")]
        [HttpGet]
        public ResponseResultInfo AddModelToCache(string openid)
        {
            GloabAccidCacheLoader Loader = new GloabAccidCacheLoader(new GloabAccidCacheHandler(),
            SqlConnectionLoader.ReadDbContextList);
            Loader.AddModelToCache(openid);
            return new ResponseResultInfo();
        }

        [Route("GetGongxparamsshengx")]
        [HttpGet]
        public GongxparamItems GetGongxparamsshengx()
        {
            var GongxparamsshengxRepository = ClassFactory.GetInstance<IGongxparamsshengxRepository>();
            return new GongxparamItems() {ParamItems = GongxparamsshengxRepository.GetGongxparamsshengx()};
        }

        [Route("InsertGongxparam")]
        [HttpPost]
       public ResponseResultInfo InsertGongxparam(GongxparamItem nitem)
        {
            var GongxparamsshengxRepository = ClassFactory.GetInstance<IGongxparamsshengxRepository>();
            GongxparamsshengxRepository.InsertGongxparamItem(nitem);
            return new ResponseResultInfo();
        }

        [Route("UpdateGongxparam")]
        [HttpPost]
        public ResponseResultInfo UpdateGongxparam(GongxparamItem nitem)
        {
            var GongxparamsshengxRepository = ClassFactory.GetInstance<IGongxparamsshengxRepository>();
            GongxparamsshengxRepository.UpdateGongxparamItem(nitem);
            return new ResponseResultInfo();
        }

        [Route("DeleteGongxparam")]
        [HttpPost]
        public ResponseResultInfo DeleteGongxparam(GongxparamItem nitem)
        {
            var GongxparamsshengxRepository = ClassFactory.GetInstance<IGongxparamsshengxRepository>();
            GongxparamsshengxRepository.DeleteAppidjoininfo(nitem);
            return new ResponseResultInfo();
        }
    }
}
